package controller.student;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import pojos.common.ReplyMessage;
import pojos.student.StudentInfo;
import service.CommonUtilsService;
import service.StudentInfoService;
import utils.InputVerifyUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = {"/student"})
public class StudentInfoController {

    @Resource(name = "studentInfoService")
    private StudentInfoService studentInfoService;

    @Resource(name = "commonUtilsService")
    private CommonUtilsService utilsService;

    /**
     * 学生修改基础信息
     */
    @PostMapping("/frame/changeBaseInfo-submit")
    @ResponseBody
    public ReplyMessage updateStudentBaseInfo(@RequestParam(value = "realName",required = false) String userName,
                                              @RequestParam(value = "sex",required = false) Integer userSex,
                                              @RequestParam(value = "school",required = false) String school,
                                              @RequestParam(value = "grade",required = false) Integer grade,
                                              HttpServletRequest request){
        HttpSession session = request.getSession();
        StudentInfo studentInfo = (StudentInfo) session.getAttribute("UserInfo");
        ReplyMessage replyMessage = new ReplyMessage();

        // 真实姓名验证
        if(userName != null && userName.length() > 0){
            if(InputVerifyUtils.isRealNameNotRight(userName)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("真实姓名不正确！");
                return replyMessage;
            }
            studentInfo.setRealName(userName);
        }

        // 性别
        if(userSex != null){
            studentInfo.setSex(userSex);
        }

        // 学校
        if(school != null && school.length() > 0){
            if(InputVerifyUtils.isSchoolNotRight(school)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("学校名称不正确！");
                return replyMessage;
            }
            studentInfo.setSchool(school);
        }

        // 年级
        if(grade != null){
            studentInfo.setGrade(grade);
        }

        replyMessage = studentInfoService.updateBaseInfo(studentInfo);
        if(replyMessage.getStatus() == 200){
            session.setAttribute("UserInfo",studentInfo);
        }
        return replyMessage;
    }

    /**
     * 学生修改登录名
     */
    @PostMapping("/frame/changeLoginName-submit")
    @ResponseBody
    public ReplyMessage updateStudentLoginName(@RequestParam(value = "loginName",required = false) String loginName,
                                               HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        HttpSession session = request.getSession();
        StudentInfo studentInfo = (StudentInfo) session.getAttribute("UserInfo");

        if(InputVerifyUtils.isLoginNameNotRight(loginName)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("登录名不正确！");
            return replyMessage;
        }

        studentInfo.setName(loginName);

        replyMessage = studentInfoService.updateStudentLoginName(studentInfo);
        if(replyMessage.getStatus() == 200){
            session.setAttribute("UserInfo",studentInfo);
        }
        return replyMessage;
    }

    /**
     * 学生修改密码
     */
    @PostMapping("/frame/changePassword-submit")
    @ResponseBody
    public ReplyMessage updateStudentPassword(@RequestParam(value = "oldPassword",required = false) String oldPassword,
                                              @RequestParam(value = "newPassword",required = false) String newPassword,
                                              HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        HttpSession session = request.getSession();
        StudentInfo studentInfo = (StudentInfo) session.getAttribute("UserInfo");
        int id = studentInfo.getId();

        if(InputVerifyUtils.isPasswordNotRight(oldPassword)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("原密码不正确！");
            return replyMessage;
        }

        if(InputVerifyUtils.isPasswordNotRight(newPassword)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("新密码不正确！");
            return replyMessage;
        }

        return studentInfoService.updateStudentPassword(oldPassword,newPassword,id);
    }

    /**
     * 学生修改手机号
     */
    @PostMapping("/frame/changeStudentPhone-submit")
    @ResponseBody
    public ReplyMessage updateStudentPhone(@RequestParam(value = "phone",required = false) String phone,
                                           @RequestParam(value = "imgCode",required = false) String imgCode,
                                           @RequestParam(value = "smsCode",required = false) String smsCode,
                                           HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        HttpSession session = request.getSession();
        StudentInfo studentInfo = (StudentInfo) session.getAttribute("UserInfo");

        if(InputVerifyUtils.isPhoneNotRight(phone)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("手机号不正确！");
            return replyMessage;
        }

        if(InputVerifyUtils.isImgAuthCodeNotRight(session,imgCode)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("图片验证码不正确！");
            return replyMessage;
        }

        if(InputVerifyUtils.isSmsCodeNotRight(session,smsCode)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("短信验证码不正确！");
            return replyMessage;
        }

        // 清空图片验证码和短信验证码Session值
        session.removeAttribute("ImgAuthCode");
        session.removeAttribute("SmsAuthCode");

        studentInfo.setPhone(phone);
        replyMessage = studentInfoService.updateStudentPhone(studentInfo);
        if(replyMessage.getStatus() == 200){
            session.setAttribute("UserInfo",studentInfo);
        }
        return replyMessage;
    }

    /**
     * 修改学生QQ号和微信号
     */
    @PostMapping("/frame/changeConnect-submit")
    @ResponseBody
    public ReplyMessage updateStudentConnect(@RequestParam(value = "QQNum",required = false) String QQNum,
                                             @RequestParam(value = "weChatNum",required = false) String weChatNum,
                                             HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        HttpSession session = request.getSession();
        StudentInfo studentInfo = (StudentInfo) session.getAttribute("UserInfo");

        if(QQNum != null && QQNum.length() > 0){
            if(InputVerifyUtils.isQQNotRight(QQNum)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("QQ号不正确！");
                return replyMessage;
            }
            studentInfo.setQQ(QQNum);
        }

        if(weChatNum != null && weChatNum.length() > 0){
            if(InputVerifyUtils.isWeChatNotRight(weChatNum)){
                replyMessage.setStatus(400);
                replyMessage.setInfo("微信号不正确！");
                return replyMessage;
            }
            studentInfo.setWeChat(weChatNum);
        }

        replyMessage = studentInfoService.updateStudentConnect(studentInfo);
        if(replyMessage.getStatus() == 200){
            session.setAttribute("UserInfo",studentInfo);
        }
        return replyMessage;
    }

    /**
     * 修改学生头像
     */
    @PostMapping("/frame/alter-student-avatar-submit")
    @ResponseBody
    public ReplyMessage alterStudentAvatar(MultipartFile avatar, HttpServletRequest request){
        ReplyMessage replyMessage = new ReplyMessage();
        if(InputVerifyUtils.isAvatarNotRight(avatar)){
            replyMessage.setStatus(400);
            replyMessage.setInfo("请选择正确的图片文件！");
            return replyMessage;
        }

        HttpSession session = request.getSession();
        StudentInfo studentInfo = (StudentInfo) session.getAttribute("UserInfo");

        replyMessage = utilsService.alterAvatar(avatar,studentInfo.getAvatar(),studentInfo.getId(),3);
        if(replyMessage.getStatus() == 200){
            studentInfo.setAvatar(replyMessage.getInfo());
        }

        return replyMessage;
    }

    /**
     * 查询学生头像
     */
    @PostMapping("/frame/query-student-avatar")
    @ResponseBody
    public ReplyMessage queryStudentAvatar(HttpServletRequest request){
        HttpSession session = request.getSession();
        StudentInfo studentInfo = (StudentInfo) session.getAttribute("UserInfo");

        ReplyMessage replyMessage = new ReplyMessage();
        replyMessage.setStatus(200);
        replyMessage.setInfo(studentInfo.getAvatar());
        return replyMessage;
    }

    /**
     * 查询学生当天课程信息
     */
    @PostMapping("/frame/query-current-class-info")
    @ResponseBody
    public ReplyMessage queryStudentCurrentClassInfo(HttpServletRequest request){
        HttpSession session = request.getSession();
        StudentInfo studentInfo = (StudentInfo) session.getAttribute("UserInfo");
        return studentInfoService.queryStudentCurrentClassInfo(studentInfo.getId());
    }
}
